package org.lds.scheduled.job.admin.starter.holder;

import org.lds.scheduled.job.common.model.AddJobModel;
import org.lds.scheduled.job.common.model.AppModel;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class JobHolder {
    private final Map<Long, AddJobModel> cache = new ConcurrentHashMap<>();

    public void save(Long id, Long appId) {
        AddJobModel addJobModel = cache.get(id);
        if (addJobModel != null &&
                addJobModel.getId().equals(id) &&
                addJobModel.getApp().getId().equals(appId)) return;
        cache.put(id, AddJobModel.builder()
                        .id(id)
                        .app(new AppModel(appId, null))
                        .build());
    }

    public void remove(Long id) {
        cache.remove(id);
    }

    public Map<Long, AddJobModel> getCache() {
        return cache;
    }
}
